/*********************************************************************************/
/*                                                                               */
/*      DMA Driver                                                               */
/*                                                                               */
/*      Last updated by:  CBS                                                    */
/*      Last update date: 12/12/25                                               */
/*      Revision:         0                                                      */
/*      Copyright:        DENSO                                                  */
/*                                                                               */
/*********************************************************************************/

/*********************************************************************************/
/*  include files                                                                */
/*********************************************************************************/
#include "MPC5674F_MVx264.h"
#include "Dma.h"

/*********************************************************************************/
/*  API Functions                                                                */
/*********************************************************************************/
void Dma_Init(const Dma_ConfigType *ConfigPtr)
{
    Dma_ChannelType channel;
    for ( channel = DMA_ADC_GROUP0_COMMAND_CHANNEL; channel < DMA_NUMBER_OF_CHANNELS; channel++ )
    {

        /* DMA channel configration*/
        EDMA_A.CPR[channel].B.ECP = ConfigPtr->dmaChannelConfigPtr[channel].DMA_CHANNEL_PREEMTION_ENABLE;
        EDMA_A.CPR[channel].B.CHPRI = ConfigPtr->dmaChannelConfigPtr[channel].DMA_CHANNEL_PRIORITY;
    }

    /* Write channel arbitration mode */
    EDMA_A.CR.B.ERCA = ConfigPtr->dmaChannelArbitration;
}

void Dma_ConfigureChannel(struct tcd_t *tcd, Dma_ChannelType channel)
{
    /* Copy transfer configuration to correct channel. */
    EDMA_A.TCD[channel] = *tcd;

    /* Disable channel. */
    EDMA_A.CERQR.R = channel;
}

volatile struct tcd_t * Dma_GetTcd( Dma_ChannelType channel )
{
    return &EDMA_A.TCD[channel];
}

void Dma_ConfigureChannelTranferSize(U4 nbrOfIterations, Dma_ChannelType channel)
{
    EDMA_A.TCD[channel].BITER = nbrOfIterations;
    EDMA_A.TCD[channel].CITER = nbrOfIterations;
}

void Dma_ConfigureChannelSourceCorr(S4 sourceCorrection, Dma_ChannelType channel)
{
    EDMA_A.TCD[channel].SLAST = sourceCorrection;
}

void Dma_ConfigureChannelDestinationCorr(S4 destinationCorrection, Dma_ChannelType channel)
{
    EDMA_A.TCD[channel].DLAST_SGA = destinationCorrection;
}

void Dma_ConfigureDestinationAddress(U4 destAddr, Dma_ChannelType channel)
{
    EDMA_A.TCD[channel].DADDR = destAddr;
}

void Dma_ConfigureSourceAddress(U4 sourceAddr, Dma_ChannelType channel)
{
    EDMA_A.TCD[channel].SADDR = sourceAddr;
}

int Dma_Active(Dma_ChannelType channel) 
{
    return EDMA_A.TCD[channel].ACTIVE;
}

void Dma_StartChannel(Dma_ChannelType channel)
{
    /* Start the channel */
    EDMA_A.SERQR.R = channel;
}

void Dma_ClearInterrupt(Dma_ChannelType channel)
{
    /* Start the channel */
    EDMA_A.CIRQR.R = channel;
}

void Dma_StopChannel(Dma_ChannelType channel)
{
    /* Stop the channel */
    EDMA_A.CERQR.R = channel;
}

Std_ReturnType Dma_ChannelDone(Dma_ChannelType channel)
{
    Std_ReturnType returnValue;

    returnValue = (U1)EDMA_A.TCD[channel].DONE;

    return (returnValue);
}

/**** End Of File ****************************************************************/
